package com.zimmur.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.zimmur.common.ConstData;
import com.zimmur.common.SpringBeanFactory;
import com.zimmur.dao.IUserRecommandDao;
import com.zimmur.dao.goods.IGoodsCollectDao;
import com.zimmur.dao.goods.IGoodsDao;
import com.zimmur.model.account.DesignerTaglib;
import com.zimmur.model.account.GoodsUser;
import com.zimmur.model.desiger.DesigerFav;
import com.zimmur.model.desiger.DesigerInfo;
import com.zimmur.model.desiger.view.DesigerVo;
import com.zimmur.model.fdfs.StorageFiles;
import com.zimmur.model.goods.Goods;
import com.zimmur.model.goods.GoodsCollect;
import com.zimmur.model.goods.GoodsColor;
import com.zimmur.model.goods.GoodsImage;
import com.zimmur.model.goods.GoodsParameter;
import com.zimmur.model.goods.GoodsType;
import com.zimmur.model.order.WishList;
import com.zimmur.model.zone.SharedProperty;
import com.zimmur.service.IMakerService;
import com.zimmur.service.IUserRecommandService;
import com.zimmur.service.IUserService;
import com.zimmur.service.RedisCacheUtil;
import com.zimmur.service.account.IDesignerTaglibService;
import com.zimmur.service.base.IAdBannerService;
import com.zimmur.service.base.UserBrowserHistoryService;
import com.zimmur.service.designer.IDesignerFavService;
import com.zimmur.service.designer.IDesignerInfoService;
import com.zimmur.service.fdfs.IStorageFilesService;
import com.zimmur.service.goods.IGoodsImageService;
import com.zimmur.service.goods.IGoodsRecommandService;
import com.zimmur.service.goods.IGoodsService;
import com.zimmur.service.goods.IGoodsSizeService;
import com.zimmur.service.goods.IGoodsTypeService;
import com.zimmur.service.order.IWishListService;
import com.zimmur.solr.model.Designer;
import com.zimmur.solr.service.ISolrDesignerService;
import com.zimmur.tools.DateUtils;
import com.zimmur.tools.PageUtil;

@Service("clomakerService")  
public class MakerServiceImpl implements IMakerService{
	private static Logger log=LoggerFactory.getLogger(MakerServiceImpl.class);
	@Resource  
	private IGoodsDao goodsDao; 
    @Resource  
    private IGoodsTypeService goodsTypeService;
    @Resource  
    private IGoodsCollectDao goodsCollectDao;
    @Resource  
    private IUserService userService;
    @Resource  
    private IWishListService wishListService;
    @Resource  
    private IUserRecommandDao userRecommandDao; 
    
	@Resource
	private IAdBannerService abBannerService;
    @Resource  
    private IUserRecommandService userRecommandService; 
    @Resource  
    private IGoodsService goodsService;
    @Resource  
    private IGoodsRecommandService goodsRecommandService;
    @Resource  
    private IGoodsImageService goodsImageService;
    @Resource  
    private IGoodsSizeService goodsSizeService;
    @Resource
	private IDesignerInfoService designerInfoService;
    @Resource
    private IDesignerTaglibService designerTaglibService;
    @Resource
    private IDesignerFavService designerFavService;
    @Resource
    private RedisCacheUtil<DesigerVo> stringRedisDao;
    @Resource
    private ISolrDesignerService solrDesignerService;
    @Resource
	private IStorageFilesService storageFilesService;
    
    private final static String REDIS_KEY ="popolook_userRecommand";
    
    private static Object syncLock = new Object();
    private static ExecutorService executorService = Executors.newFixedThreadPool(10);
    
	@Override
	public PageUtil<Goods> goodsQuery(long userId,GoodsParameter goodsParameter,Integer pageNo) {
		List<Goods> goodsList =new ArrayList<>();
		PageUtil<Goods> page =new PageUtil<>();
		try {
			if(pageNo == 0){
				pageNo=1;
			}
			int totalRecord = 0;
			goodsParameter.setPageNo(pageNo);
			page.setPageNo(pageNo);			
			
			List<Goods> goodsListS=null;
			List<String> typeIds=new ArrayList<>();
			//按类别类型（1衣匠2家定）根据0层类别ID查询商品
			if(goodsParameter.getTypeFlag() > 0){
				if(goodsParameter.getTypeId() > 0 && goodsParameter.getParentTypeId() == 0){
					//按商品类型查询出商品 
					List<GoodsType> goodsTypeList= goodsTypeService.selectByParentTypeIdGoodsType(goodsParameter);
					if(!goodsTypeList.isEmpty() && goodsTypeList.size() > 0){
						typeIds.add(goodsParameter.getTypeId().toString());
						for(GoodsType goodsTypeItem :goodsTypeList){
							typeIds.add(goodsTypeItem.getTypeId().toString());
						}
						goodsParameter.setTypeIds(typeIds);
					}
				}
				//上一层ID不为0的时候，就按1层typeId查询商品			 
				else if(goodsParameter.getTypeId() > 0  && goodsParameter.getParentTypeId() > 0){
					typeIds.add(goodsParameter.getTypeId().toString());
					goodsParameter.setTypeIds(typeIds);
				}
				//总数量
				totalRecord = goodsService.countGoodsByParam(goodsParameter);
				if(totalRecord>0){
					goodsListS = goodsService.selectGoodsByParam(goodsParameter);
				}
			}
			//根据商品获取详细信息
			if(goodsListS!=null && goodsListS.size()>0){
				for(Goods goodsItem : goodsListS){
					//商品图片
					List<GoodsImage> goodsImageList = goodsImageService.selectByGoodsId(Long.parseLong(goodsItem.getGoodsId()));
					goodsItem.setGoodsImages(goodsImageList);
					//商品创建人信息
					GoodsUser user =userService.selectGoodsIsConcern(userId,goodsItem.getUserId());
			 		if(user != null){
			 			DesigerInfo desigerInfo = designerInfoService.selectByUserId(user.getUserId());
						if(desigerInfo != null){
							user.setTags(desigerInfo.getTags());
						}
			 			goodsItem.setUser(user);
			 		}
			 		goodsList.add(goodsItem);
				}
			}
			page.setTotalRecord(totalRecord);
			page.setItemList(goodsList);
		} catch (Exception e) {
			log.error(e.getMessage(),e);
		}
		return page;
	}
    
	@Override
	public List<Goods> goodsTypeSelect(Integer typeId) {
		List<Goods> goodsList=null;
		try {
			GoodsParameter goodsParameter = new GoodsParameter();
			goodsParameter.setTypeId(typeId);
			//按商品类型查询出商品 
			List<GoodsType> goodsTypeList= goodsTypeService.selectByParentTypeIdGoodsType(goodsParameter);
			if(!goodsTypeList.isEmpty() && goodsTypeList.size() > 0){
				List<String> typeIds=new ArrayList<>();
				for(GoodsType goodsTypeItem :goodsTypeList){
					typeIds.add(goodsTypeItem.getTypeId().toString());
				}
				goodsParameter.setTypeIds(typeIds);
				goodsParameter.setPageSize(null);
				goodsList = goodsService.selectListByTypeIds(goodsParameter);
			}
		} catch (Exception e) {
			log.error(e.getMessage());
		}
		return goodsList;
	}

	@Override
	public Map<String, Object> MakerIndex(Long createUser,Integer pageNow) {
		Map<String, Object> map = new HashMap<>();
		try {
			SharedProperty sp=new SharedProperty();	

			/*//轮播图，广告信息:10=公用模块/20=账号管理/30=社区模块/40=衣匠模块/50=订单模块/60=家定模块
			List<AdBanner> adBannerList = abBannerService.selectAdList(Integer.parseInt(AppModuleEnum.ClothsMaker.getModuleId()));
			if(!adBannerList.isEmpty() && adBannerList.size() > 0){
				map.put("adBannerList", adBannerList);
			}*/
			//设计师标签库
			List<DesignerTaglib> designerTaglibsList = designerTaglibService.selectList(0);
			if(!designerTaglibsList.isEmpty()){
				map.put("taglibs", designerTaglibsList);
			}
		
			PageUtil<Designer> desigerVos = MakerQueryWithKeyAndTagsBySolr("","", createUser, pageNow);
				
			if(!desigerVos.getItemList().isEmpty()){
				map.put("desigerVos", desigerVos);
			}
		} catch (Exception e) {
			log.error(e.getMessage());
		}
		return map;
	}
	

	@Override
	public PageUtil<DesigerVo> MakerPageByKeyAndTags(String key,String tags, Long createUser, Integer pageNow) {
		if(pageNow == 0){
			pageNow=1;
		}
		String redis_key = REDIS_KEY+"_"+key+"_"+tags+"_"+pageNow+"_MakerPageByKeyAndTags";
		String redis_key_count = REDIS_KEY+"_"+key+"_"+tags+"_count_MakerPageByKeyAndTags";		
		List<DesigerVo> desigerVos = new ArrayList<>();
		PageUtil<DesigerVo> page = new PageUtil<>();
		Integer totalRecord = 0;
		boolean countExist=false;
		//总数量
		try {
			countExist=stringRedisDao.exists(redis_key_count);
		} catch (Exception e) {
			log.error(e.getMessage());
		}
		if(countExist){
			totalRecord = Integer.parseInt(stringRedisDao.getCacheString(redis_key_count));
		}else{
			synchronized (syncLock) {
				try {
					countExist=stringRedisDao.exists(redis_key_count);
				} catch (Exception e) {
					log.error(e.getMessage());
				}
				if(countExist){
					totalRecord = Integer.parseInt(stringRedisDao.getCacheString(redis_key_count));
				}else{
				
					totalRecord = designerInfoService.countDesiger(key,tags);
					if (totalRecord  != null) {
						page.setTotalRecord(totalRecord);
						page.setPageNo(pageNow);
					}
					try {
						stringRedisDao.setCacheString(redis_key_count, String.valueOf(totalRecord));
						stringRedisDao.setTimeout(redis_key_count, ConstData.REDIS_KEY_TIMEOUT);
					} catch (Exception e) {
						log.error(e.getMessage());
					}
				}
			}
		}
		//衣匠
		boolean makereExist=false;
		try {
			makereExist=stringRedisDao.exists(redis_key);
		} catch (Exception e) {
			log.error(e.getMessage());
		}
		if(makereExist){
			desigerVos = stringRedisDao.getCacheList(redis_key);
		}else{
			synchronized (syncLock) {
				try {
					makereExist=stringRedisDao.exists(redis_key);
				} catch (Exception e) {
					log.error(e.getMessage());
				}
				if(makereExist){
					desigerVos = stringRedisDao.getCacheList(redis_key);
				}else{
					desigerVos=designerInfoService.selectDesiger(key, tags,pageNow, page.getPageSize(), createUser);
					try {
						stringRedisDao.setCacheList(redis_key, desigerVos);
						stringRedisDao.setTimeout(redis_key, ConstData.REDIS_KEY_TIMEOUT);
					} catch (Exception e) {
						log.error(e.getMessage());
					}
				}
			}
		}
		if(desigerVos != null && desigerVos.size()>0){
			try {
				for(DesigerVo desigerVo:desigerVos){
					DesigerFav desigerFav=designerFavService.selectDesigerFav(createUser, desigerVo.getUserId());
					if(desigerFav!=null){
						desigerVo.setIsConcern(desigerFav.getStatus().intValue());						
					}
				}
			} catch (Exception e) {
				log.error(e.getMessage());
			}
			page.setItemList(desigerVos);
		}
		page.setPageNo(pageNow);
		page.setTotalRecord(totalRecord);
		
		return page;
	}

	@Override
	public List<Goods> desiGnerIndex(long userId,Integer pageNo) {
//		GoodsExample goodsExample =new GoodsExample();
		List<Goods> goodsList = new ArrayList<>();
		try {
			//设计师信息
//			GoodsUser user =userService.selectByUserId(userId);
//			if(user != null){
//				goodsExample.setUser(user);
//			}
			if(pageNo == 0){
				pageNo=1;
			}
			//设计师的所有作品
			PageUtil<Goods>page = new PageUtil<>();
			page.setUserId(userId);
			page.setPageNo(pageNo);
	 		GoodsParameter goodsParameter = new GoodsParameter();
	 		goodsParameter.setUserId(userId);
	 		goodsParameter.setPageNo(pageNo);
			goodsList = goodsDao.selectListByUserId(goodsParameter);
			if(!goodsList.isEmpty() && goodsList.size() > 0){
				for(Goods goodsItem : goodsList){
					//商品图片
					List<GoodsImage> goodsImageList = goodsImageService.selectByGoodsId(Long.parseLong(goodsItem.getGoodsId()));
					goodsItem.setGoodsImages(goodsImageList);
				}
//				goodsExample.setGoodss(goodsList);
			}
		} catch (Exception e) {
			log.error(e.getMessage());
		}
		return goodsList;
	}
	
	@Override
	public PageUtil<Goods> desigerGoodsPage(String key,long userId,Integer pageNow) {
		List<Goods> goodsList =new ArrayList<>();
		PageUtil<Goods> page = new PageUtil<>();
		try {
			if(pageNow == 0){
				pageNow=1;
			}
			Integer totalRecord = goodsService.selectUserIdCountGoods(userId);
			if(totalRecord > 0){
				page.setTotalRecord(totalRecord);
				page.setPageNo(pageNow);
		 		GoodsParameter goodsParameter = new GoodsParameter();
		 		goodsParameter.setUserId(userId);
		 		goodsParameter.setPageNo(pageNow);
		 		Short typeFlag=1;
		 		goodsParameter.setTypeFlag(typeFlag);
		 		if(!key.equals("")){
		 			goodsParameter.setTitle(key);
		 		}
				//设计师的所有作品
				List<Goods> goodsListS = goodsDao.selectListByUserId(goodsParameter);
				if(!goodsListS.isEmpty() && goodsListS.size() > 0){
					for(Goods goodsItem : goodsListS){
						//商品图片
						List<GoodsImage> goodsImageList = goodsImageService.selectByGoodsId(Long.parseLong(goodsItem.getGoodsId()));
						goodsItem.setGoodsImages(goodsImageList);
						//商品创建人信息
						GoodsUser user =userService.selectByUserId(goodsItem.getUserId());
				 		if(user != null){
				 			goodsItem.setUser(user);
				 		}
						goodsList.add(goodsItem);
					}
					page.setItemList(goodsList);
				}
			}
		} catch (Exception e) {
			log.error(e.getMessage());
		}
		return page;
	}

	@Override
	public PageUtil<Goods> thesHelves(long userId,Integer pageNow) {
//		GoodsExample goodsExample = new GoodsExample();
		List<Goods> goodsList = new ArrayList<>();
		PageUtil<Goods> page = new PageUtil<>();
		try {
			Integer totalRecord = goodsDao.selectUserIdInterval30CountGoods(userId);
			if(totalRecord > 0){
				//获取当前时间
		        Integer timeDate =DateUtils.getUnixTimeByDate(DateUtils.getNow());
		        /*获取一个月前的时间*/
				Integer publishDate =DateUtils.getMonthBeforeDateNum(DateUtils.getDateByUnixTime(timeDate));
		 		if(pageNow == 0){
					pageNow=1;
				}
		 		GoodsParameter goodsParameter = new GoodsParameter();
		 		goodsParameter.setPublishDate(publishDate);
		 		goodsParameter.setUserId(userId);
		 		goodsParameter.setPageNo(pageNow);
	
				goodsList = goodsDao.selectByUserIdRecentList(goodsParameter);
				if(!goodsList.isEmpty() && goodsList.size() > 0){
					page.setItemList(goodsList);
		 		}
			}
		} catch (Exception e) {
			log.error(e.getMessage());
		}
		return page;
	}
	//最新商品
	@Override
	public PageUtil<Goods> shelvesPage(String key,long userId, Integer pageNow) {
		PageUtil<Goods> page = new PageUtil<>();
		try {
			if(pageNow == 0){
				pageNow=1;
			}
			//获取当前时间
//	        Integer timeDate =DateUtils.getUnixTimeByDate(DateUtils.getNow());
	        /*获取一个月前的时间*/
	        Integer publishDate =DateUtils.getAfterdayUnixTime(-30);
//			Integer publishDate =DateUtils.getMonthBeforeDateNum(DateUtils.getDateByUnixTime(timeDate));
			//用户所有的商品总记录数
			Integer totalRecord = goodsDao.selectUserIdInterval30CountGoods(userId);
			if(totalRecord > 0){
		 		GoodsParameter goodsParameter = new GoodsParameter();
		 		goodsParameter.setTotalRecord(totalRecord);
		 		goodsParameter.setPublishDate(publishDate);
		 		Short typeFlag=1;
		 		goodsParameter.setTypeFlag(typeFlag);
		 		goodsParameter.setUserId(userId);
		 		if(!key.equals("")){
		 			goodsParameter.setTitle(key);
		 		}
		 		goodsParameter.setPageNo(pageNow);
				//设计师的所有作品
		 		page.setTotalRecord(totalRecord);
		 		page.setPageNo(pageNow);
				List<Goods> goodsListS = goodsDao.selectByUserIdRecentList(goodsParameter);
				List<Goods> goodsList = new ArrayList<>();
				if(!goodsListS.isEmpty() && goodsListS.size() > 0){
					for(Goods goodsItem : goodsListS){
						//商品图片
						List<GoodsImage> goodsImageList = goodsImageService.selectByGoodsId(Long.parseLong(goodsItem.getGoodsId()));
						goodsItem.setGoodsImages(goodsImageList);
						//商品创建人信息
						GoodsUser user =userService.selectByUserId(goodsItem.getUserId());
				 		if(user != null){
				 			goodsItem.setUser(user);
				 		}
				 		goodsList.add(goodsItem);
					}
					page.setItemList(goodsList);
				}
			}
		} catch (Exception e) {
			log.error(e.getMessage());
		}
		return page;
	}
	
	@Override
	public PageUtil<Goods> commUnityGoods(Long userId,String key,Integer pageNow) {
		List<Goods> goodsList=new ArrayList<>();
		PageUtil<Goods> page = new PageUtil<>();
		try {
			GoodsParameter goodsParameter = new GoodsParameter();
			Short typeFlag=1;
			goodsParameter.setTypeFlag(typeFlag);
			goodsParameter.setUserId(userId);
			Integer totalRecord = goodsService.selectByDesignerIdAndTypeFlagForZoneRecommandCount(goodsParameter);
			if(totalRecord > 0){
		 		page.setTotalRecord(totalRecord);
		 		page.setPageNo(pageNow);
		 		goodsParameter.setPageNo(pageNow);
				
		 		goodsList = goodsService.selectByDesignerIdAndTypeFlagForZoneRecommand(goodsParameter);
				if(!goodsList.isEmpty()){
					for(Goods goodsItem : goodsList){
						GoodsUser user = userService.selectByUserId(goodsItem.getUserId());
						goodsItem.setUser(user);
						//商品图片
						List<GoodsImage> goodsImageList = goodsImageService.selectByGoodsId(Long.parseLong(goodsItem.getGoodsId()));
						goodsItem.setGoodsImages(goodsImageList);
					}
					page.setItemList(goodsList);
		 		}
			}
		} catch (Exception e) {
			log.error(e.getMessage());
		}
		return page;
	}

	@Override
	public int updateGoodsShareNum(String share, long goodsId) {
		Integer resut=0;
		try {
			Goods goods= goodsDao.selectByPrimaryKey(goodsId);
			if(goods != null){
				//转发
				Goods Goodsc =new Goods();
				Integer shareNum=goods.getShareNum()+1;
				Goodsc.setShareNum(shareNum);
				Goodsc.setGoodsId(goodsId+"");
				resut=goodsDao.updateGoodsShareNum(Goodsc);
				return resut;
			}
		} catch (Exception e) {
			log.error(e.getMessage());
		}
		return resut;
	}

	@Override
	public Goods goodsDetails(long goodsId,long userId,Boolean isUser) {
		Goods goods= null;
		try {
			goods = goodsService.selectByGoodsIdAndIsCollect(userId, goodsId);
			if(goods != null){
				/**
				 * 增加用户浏览记录
				 */
				if(isUser){
					addUserBrowserLog(goodsId,userId);
				}
				//1.是否收藏
				GoodsCollect goodsCollect = new GoodsCollect();
				goodsCollect.setUserId(userId);
				goodsCollect.setGoodsId(goodsId);
				goodsCollect = goodsCollectDao.selectGoodsCollect(goodsCollect);
				if(goodsCollect != null){
					goods.setIsCollect(goodsCollect.getStatus());
				}
				//
				StorageFiles storageFiles = storageFilesService.selectByFileId(Long.parseLong(goods.getCoverImage()));
				if(storageFiles!=null){
					goods.setCoverImageUrl("/"+storageFiles.getStoragegroup()+"/"+storageFiles.getStorageid());
				}
				//2.判断是否添加愿望单
				if(isUser){
					WishList wl  = wishListService.selectByGoodsIdAndUserId(userId, goodsId);
					if(wl!=null){
						goods.setIsWish(1);
					}
				}
				//3.商品图片信息
				List<GoodsImage> goodsImageList = goodsImageService.selectByGoodsId(Long.parseLong(goods.getGoodsId()));
				if(!goodsImageList.isEmpty() && goodsImageList.size() > 0){
					goods.setGoodsImages(goodsImageList);
				}
				
				//4.商品用户信息
				GoodsUser user = userService.selectByUserId(goods.getUserId());
				if(user != null){
					goods.setUser(user);
				}
				//5.商品颜色
				List<GoodsColor> goodsColors = new ArrayList<GoodsColor>();
				goodsColors =goodsSizeService.selectGoodsColorByGoodsId(Long.parseLong(goods.getGoodsId()));
				if(!goodsColors.isEmpty() && goodsColors.size() > 0){					
					/*for (GoodsColor goodsColor : goodsColors) {
						List<GoodsSize> goodsSizes = goodsSizeService.selectByGoodsId(goodsId);
					}*/
					goods.setGoodsColorList(goodsColors);					
				}else{
					GoodsColor goodsColor = new GoodsColor();
					goodsColors.add(goodsColor);
					goods.setGoodsColorList(goodsColors);
				}
				/*//商品尺码信息
				List<GoodsSize> goodsSizeList = goodsSizeService.selectByGoodsId(goods.getGoodsId());
				if(!goodsSizeList.isEmpty() && goodsSizeList.size() > 0){
					goods.setGoodsSizeList(goodsSizeList);
				}*/
				//6.库存为0，为售罄状态
				Integer inventoryNum=goods.getInventoryNum();
				if(inventoryNum == null || inventoryNum==0){
					goods.setStatus("none");
				}
				//7.更新浏览数
				int num = goods.getBrowerNum()==null ?0:goods.getBrowerNum();
				goods.setBrowerNum(num+1);
				goodsService.updateBrowersNum(goods);
				
			}
		} catch (Exception e) {
			log.error(e.getMessage(),e);
		}
		return goods;
	}

	private void addUserBrowserLog(final long goodsId,final long userId){
		Future<Boolean> submit = executorService.submit(new Callable<Boolean>() {

			@Override
			public Boolean call() throws Exception {
				UserBrowserHistoryService bean = SpringBeanFactory.getBean(UserBrowserHistoryService.class);
				return bean.recordGoodsHistory(userId+"", goodsId+"");
			}
			
		});
		try {
			submit.get();
		} catch (InterruptedException e) {
			log.error("addUserBrowserLog Error", e);
		} catch (ExecutionException e) {
			log.error("addUserBrowserLog Error", e);
		}
	}
	
	@Override
	public int collectGoods(GoodsCollect goodsCollects) {
		Integer resut=0;
		try {
			GoodsCollect GoodsCollect = goodsCollectDao.selectGoodsCollect(goodsCollects);
		  
//			long goodsId = goodsCollects.getGoodsId();
//			Goods goods = goodsDao.selectByPrimaryKey(goodsId);
	        if(null == GoodsCollect){
			    resut= goodsCollectDao.insertGoodsCollect(goodsCollects);
	        }
	        if(null !=  GoodsCollect && GoodsCollect.getStatus().equals(1)){
	        	goodsCollects.setStatus(0);
	        	resut= goodsCollectDao.updateGoodsCollect(goodsCollects);
	        }
	        if(null !=  GoodsCollect && GoodsCollect.getStatus().equals(0)){
	        	goodsCollects.setStatus(1);
	        	resut= goodsCollectDao.updateGoodsCollect(goodsCollects);
	        }
//	        goodsDao.updateGoodsFavNum(goods);
			
		} catch (Exception e) {
			log.error(e.getMessage());
		}
		return resut;
	}

	@Override
	public List<GoodsType> allGoodsType(GoodsParameter goodsParameter) {
		List<GoodsType> goodsTypeList=new ArrayList<>();
		try {
	//		GoodsParameter goodsParameter =new GoodsParameter();
			if(goodsParameter != null){
				if(goodsParameter.getTypeFlag() > 0 && goodsParameter.getTypeId() == 0){
					//商品类别 （一层）信息 typeFlag=#{typeFlag} and parentTypeId=0
					goodsTypeList =goodsTypeService.selectByTypeFlagGoodsType(goodsParameter.getTypeFlag());//(typeFlag);
				}
				if(goodsParameter.getTypeId() > 0){
					//按商品类型查询出（下层）类别商品信息  parentTypeId=#{typeId}  and typeFlag=#{typeFlag} 
					goodsTypeList= goodsTypeService.selectByParentTypeIdGoodsType(goodsParameter);
				}
			}
		} catch (Exception e) {
			log.error(e.getMessage());
		}
		return goodsTypeList;
	}

	@Override
	public PageUtil<Goods> myCollectGoods(long userId,Integer pageNo) {
		List<Goods> goodsList=new ArrayList<>();
		PageUtil<Goods>page =new PageUtil<>();
		PageUtil<GoodsCollect>page1 =new PageUtil<>();
		try {
			if(pageNo == 0){
				pageNo=1;
			}
			page1.setPageNo(pageNo);
			page1.setUserId(userId);
			Integer totalRecord = goodsCollectDao.selectByUserIdGoodsCollectCount(userId);
			if(totalRecord >0){
				page.setPageNo(pageNo);
				page.setTotalRecord(totalRecord);
			}
			/*List<GoodsCollect> goodsCollectList =goodsCollectDao.selectByUserIdGoodsCollect(page1);
			if(!goodsCollectList.isEmpty() && goodsCollectList.size() > 0){
				List<Long> goodsIds=new ArrayList<>();
				for(GoodsCollect goodsCollectItem :goodsCollectList){
					goodsIds.add(goodsCollectItem.getGoodsId());
				}
				
			}	*/
			//商品信息
			List<Goods> userGoodsList = goodsService.selectUserCollectionByGoodsIds(userId, pageNo);
			for(Goods goodsItem : userGoodsList){
				//商品图片信息
				List<GoodsImage> goodsImageList = goodsImageService.selectByGoodsId(Long.parseLong(goodsItem.getGoodsId()));
				if(!goodsImageList.isEmpty() && goodsImageList.size() > 0){
					goodsItem.setGoodsImages(goodsImageList);
				}
		 		goodsList.add(goodsItem);
			}
			if(!goodsList.isEmpty() && goodsList.size() > 0){
				page.setItemList(goodsList);
				page.setTotalRecord(totalRecord);
			}
		} catch (Exception e) {
			log.error(e.getMessage());
		}
		return page;
	}

	@Override
	public PageUtil<com.zimmur.solr.model.Goods> goodsQueryBySolr(long userId, 
			GoodsParameter goodsParameter, Integer pageNo) {
		List<com.zimmur.solr.model.Goods> goodsList =new ArrayList<>();
		PageUtil<com.zimmur.solr.model.Goods> page =new PageUtil<>();
		try {
			if(pageNo == 0){
				pageNo=1;
			}
			int totalRecord = 0;
			goodsParameter.setPageNo(pageNo);
			page.setPageNo(pageNo);			
			page=goodsService.queryGoodsBySolr(goodsParameter);
			List<com.zimmur.solr.model.Goods> goodsListS=page.getItemList();
			
			//根据商品获取详细信息
			if(goodsListS!=null && goodsListS.size()>0){
				for(com.zimmur.solr.model.Goods goodsItem : goodsListS){
					//商品图片
					List<GoodsImage> goodsImageList = goodsImageService.selectByGoodsId(Long.parseLong(goodsItem.getGoodsId()));
					goodsItem.setGoodsImages(goodsImageList);
					//商品创建人信息
					GoodsUser user =userService.selectGoodsIsConcern(userId,goodsItem.getUserId());
			 		if(user != null){
			 			DesigerInfo desigerInfo = designerInfoService.selectByUserId(user.getUserId());
						if(desigerInfo != null){
							user.setTags(desigerInfo.getTags());
						}
			 			goodsItem.setUser(user);
			 		}
			 		goodsList.add(goodsItem);
				}
			}
			page.setItemList(goodsList);
		} catch (Exception e) {
			log.error(e.getMessage(),e);
		}
		return page;
	}

	@Override
	public PageUtil<Designer> MakerQueryWithKeyAndTagsBySolr(String key, String tags, Long createUser,Integer pageNow) {
		if(pageNow==0)
			pageNow=1;
		PageUtil<Designer> page=solrDesignerService.queryBytabAndkey(tags, key, pageNow);
		page.setPageNo(pageNow);
		List<Designer> designers=page.getItemList();
		if(designers != null && designers.size()>0){
			try {
				for(Designer designer:designers){
					DesigerFav desigerFav=designerFavService.selectDesigerFav(createUser, designer.getUserId());
					if(desigerFav!=null){
						designer.setIsConcern(desigerFav.getStatus().intValue());						
					}
				}
			} catch (Exception e) {
				log.error(e.getMessage());
			}
		}
		return page;
	}


	/*@Override
	public PageUtil<GoodsView> cloMakerQuery(String key, Long createUser, Integer pageNow) {
		List<GoodsView> goodsViewList =new ArrayList<>();
		PageUtil<GoodsView> page = new PageUtil<>();
		GoodsView goodsView = new GoodsView();
		try {
			if(pageNow == 0){
				pageNow=1;
			}
			
    		Integer totalRecord = designerInfoService.selectByTagsAndNameCount(key);
    		if(totalRecord > 0){
        		page.setTotalRecord(totalRecord);
        		page.setPageNo(pageNow);
    		}
    		
    		GoodsParameter goodsParameter = new GoodsParameter();
    		goodsParameter.setTypeFlag(Short.parseShort("1"));
			goodsParameter.setPageNo(0);
			Integer saleNum=0;
			
    		List<DesigerInfo> desigerInfos = designerInfoService.selectByTagsAndNameList(key, pageNow, page.getPageSize());
    		for (DesigerInfo desigerInfo : desigerInfos) {
    			goodsView = new GoodsView();
    			GoodsUser user = userService.selectGoodsIsConcern(createUser, desigerInfo.getUserId());
    			
    			if(user != null){
    				user.setTags(desigerInfo.getTags());
    			}
    			//获取推荐商品
				List<GoodsRecommand> goodsRecommandList= goodsRecommandService.selectByUserIdGoodsRecommandList(desigerInfo.getUserId());
				List<Long> goodsIds=new ArrayList<>();
				if(!goodsRecommandList.isEmpty() && goodsRecommandList.size() > 0){
					for(GoodsRecommand goodsRecommandItem :goodsRecommandList){
						goodsIds.add(goodsRecommandItem.getGoodsId());
					}
				}
				List<Goods> goodsList=new ArrayList<>();
				goodsParameter.setUserId(desigerInfo.getUserId());
				//商品信息
				List<Goods> goodsListS = goodsService.selectByDesignerIdTop10(goodsParameter);
				if(!goodsListS.isEmpty()){
					for(Goods goodsItem : goodsListS){							
						saleNum+=goodsItem.getSaleNum();
						//商品图片
						List<GoodsImage> goodsImageList = goodsImageService.selectByGoodsId(goodsItem.getGoodsId());
						goodsItem.setGoodsImages(goodsImageList);
						//添加衣匠信息
						goodsItem.setUser(user);
				
						goodsList.add(goodsItem);						
					}
					goodsView.setGoodsList(goodsList);
					user.setSaleNum(saleNum);
				}
				goodsView.setDesignerUser(user);
				
				goodsViewList.add(goodsView);
				saleNum = 0;
				goodsListS = null;
			}
    									
    		if(goodsView != null){
				page.setItemList(goodsViewList);
			}
		} catch (Exception e) {
			log.error(e.getMessage());
		}
		return page;
	}*/
	/**
	 * 按设计师标签查找
	 *//*
	public PageUtil<DesigerVo> selectByDesignerTags(String tags, long CreateUserId, int pageNow) {
		List<GoodsView> goodsViewList =new ArrayList<>();
		PageUtil<DesigerVo> page = new PageUtil<>();
		GoodsView goodsView;
		try {
			if(pageNow == 0){
				pageNow=1;
			}
			
			SharedProperty sp=new SharedProperty();
	    	sp.setPageNow(pageNow);			
	    	
			if(!tags.equals("")){
		    	sp.setCreateUser(userId);
	    		sp.setTags(tags);
			}
    		Integer totalRecord = designerInfoService.selectByTagsCount(tags);
    		if(totalRecord > 0){
        		page.setTotalRecord(totalRecord);
        		page.setPageNo(pageNow);
    		}
    		List<DesigerInfo> userList = designerInfoService.selectByTags(tags, pageNow-1, sp.getPageSize());
    		GoodsParameter goodsParameter = new GoodsParameter();
    		goodsParameter.setTypeFlag((short)1);
    		goodsParameter.setPageBeginNo(0);
    		goodsParameter.setPageSize(10);
    		
    		Integer saleNum=0;
    		for(DesigerInfo user : userList){
	    		goodsView = new GoodsView();
	    		
	    		goodsParameter.setUserId(user.getUserId());
	    		
				List<Goods> goodsList= goodsService.selectByDesignerIdTop10(goodsParameter);//****
				
				GoodsUser goodsUser = userService.selectGoodsIsConcern(userId, user.getUserId());
				goodsUser.setTags(user.getTags());
				goodsUser.setSaleNum(saleNum);
				
				goodsView.setDesignerUser(goodsUser);
				
			
				if(goodsList!=null && goodsList.size()>0){
					for(Goods goodsItem : goodsList){							
						saleNum+=goodsItem.getSaleNum();
						//商品图片
						List<GoodsImage> goodsImageList = goodsImageService.selectByGoodsId(goodsItem.getGoodsId());
						goodsItem.setGoodsImages(goodsImageList);
						
						goodsItem.setUser(goodsUser);
						
					}
				}				
				goodsView.setGoodsList(goodsList);
				
				goodsViewList.add(goodsView);
				saleNum = 0;
    		}
    		
    		if(!goodsViewList.isEmpty()){
				page.setItemList(goodsViewList);
			}
		} catch (Exception e) {
			log.error(e.getMessage());
		}
		return page;
	}*/



}
